package top.codedance.iotp.common.entity;

import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;

public class RegistServerMeta extends ServerMeta implements Serializable {

    private int clientCount = 0;

    public RegistServerMeta(ServerMeta sm) {
        this.setBindIp(sm.getBindIp());
        this.setHttpBindHost(sm.getHttpBindHost());
        this.setWsBindHost(sm.getWsBindHost());
        this.setTcpPort(sm.getTcpPort());
        this.setHttpPort(sm.getHttpPort());
        this.setWsPort(sm.getWsPort());
        this.setNetUsername(sm.getNetUsername());
        this.setNetPassword(sm.getNetPassword());
        this.setWeight(sm.getWeight());
    }

    public RegistServerMeta(String bindIp, String httpBindHost, String wsBindHost, int tcpPort, int httpPort, int wsPort, String netUsername, String netPassword, int clientCount) {
        super(bindIp, httpBindHost, wsBindHost, tcpPort, httpPort, wsPort, netUsername, netPassword);
        this.clientCount = clientCount;
    }

    public RegistServerMeta(String bindIp, String httpBindHost, String wsBindHost, int tcpPort, int httpPort, int wsPort, String netUsername, String netPassword, int weight, int clientCount) {
        super(bindIp, httpBindHost, wsBindHost, tcpPort, httpPort, wsPort, netUsername, netPassword, weight);
        this.clientCount = clientCount;
    }

    @Override
    public JSONObject getJSONObject() {
        JSONObject jsonObject = new JSONObject();
        Field[] fields = getClass().getSuperclass().getDeclaredFields();
        for (Field f : fields){
            if(Arrays.asList(new String[]{"netUsername", "netPassword"}).contains(f.getName())){
                continue;
            }
            try {
                f.setAccessible(true);
                jsonObject.put(f.getName(), f.get(this));
            }catch(Exception e){}
        }
        Field[] fields2 = getClass().getDeclaredFields();
        for (Field f : fields2){
            try {
                f.setAccessible(true);
                jsonObject.put(f.getName(), f.get(this));
            }catch(Exception e){}
        }
        return jsonObject;
    }

    public int getClientCount() {
        return clientCount;
    }

    public void setClientCount(int clientCount) {
        this.clientCount = clientCount;
    }

    public static RegistServerMeta parse(String jsonString){
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        return new RegistServerMeta(
                jsonObject.getString("bindIp"),
                jsonObject.getString("httpBindHost"),
                jsonObject.getString("wsBindHost"),
                jsonObject.getIntValue("tcpPort"),
                jsonObject.getIntValue("httpPort"),
                jsonObject.getIntValue("wsPort"),
                jsonObject.getString("netUsername"),
                jsonObject.getString("netPassword"),
                jsonObject.getIntValue("weight"),
                jsonObject.getIntValue("clientCount")
        );
    }
}
